/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.zx.user.vip.entity.TuserVip;
import com.jeesite.modules.zx.user.vip.service.TuserVipService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.zx.user.entity.ZxUser;
import com.jeesite.modules.zx.user.service.ZxUserService;

/**
 * 用户表Controller
 * @author zpp
 * @version 2020-01-29
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/user/zxUser")
public class ZxUserController extends BaseController {

	@Autowired
	private ZxUserService zxUserService;

	@Autowired
	private TuserVipService tuserVipService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZxUser get(String userCode, boolean isNewRecord) {
		return zxUserService.get(userCode, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:user:zxUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(ZxUser zxUser, Model model) {
		model.addAttribute("zxUser", zxUser);
		return "modules/zx/user/zxUserList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:user:zxUser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZxUser> listData(ZxUser zxUser, HttpServletRequest request, HttpServletResponse response) {
		List<String> list=new ArrayList<>();
		list.add("student");
		list.add("teacher");
		zxUser.getSqlMap().getWhere().and("identity", QueryType.IN, list);
		zxUser.setPage(new Page<>(request, response));
		Page<ZxUser> page = zxUserService.findPage(zxUser);
		return page;
	}

	/**
	 * 查询列表数据 过滤非会员数据
	 */
	@RequestMapping(value = "viplistData")
	@ResponseBody
	public Page<ZxUser> viplistData(ZxUser zxUser, HttpServletRequest request, HttpServletResponse response) {

		TuserVip userVip = new TuserVip();
		List<String> list=new ArrayList<>();
		list.add("student");
		list.add("teacher");
		zxUser.getSqlMap().getWhere().and("identity", QueryType.IN, list);
		userVip.setZxuser(zxUser);
		userVip.setPage(new Page<>(request, response));
		Page<TuserVip> vipPage = tuserVipService.findPage(userVip);
		List<TuserVip> tuserVipList = vipPage.getList();
		Page<ZxUser> page = new Page<>();
		List<ZxUser> zxUserList = new ArrayList<>();
		if (tuserVipList.size()>0){
			tuserVipList.forEach(item->{
				zxUserList.add(item.getZxuser());
			});
			page.setCount(vipPage.getCount());
			page.setList(zxUserList);
			page.setPageNo(vipPage.getPageNo());
			page.setPageSize(vipPage.getPageSize());
		}
		return page;
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:user:zxUser:view")
	@RequestMapping(value = "form")
	public String form(ZxUser zxUser, Model model) {
		model.addAttribute("zxUser", zxUser);
		return "modules/zx/user/zxUserForm";
	}

	/**
	 * 保存用户表
	 */
	@RequiresPermissions("zx:user:zxUser:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZxUser zxUser) {
		zxUserService.save(zxUser);
		return renderResult(Global.TRUE, text("保存用户表成功！"));
	}

	/**
	 * 停用用户表
	 */
	@RequiresPermissions("zx:user:zxUser:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ZxUser zxUser) {
		zxUser.setStatus(ZxUser.STATUS_DISABLE);
		zxUserService.updateStatus(zxUser);
		return renderResult(Global.TRUE, text("停用用户表成功"));
	}

	/**
	 * 启用用户表
	 */
	@RequiresPermissions("zx:user:zxUser:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ZxUser zxUser) {
		zxUser.setStatus(ZxUser.STATUS_NORMAL);
		zxUserService.updateStatus(zxUser);
		return renderResult(Global.TRUE, text("启用用户表成功"));
	}

	/**
	 * 删除用户表
	 */
	@RequiresPermissions("zx:user:zxUser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZxUser zxUser) {
		zxUserService.delete(zxUser);
		return renderResult(Global.TRUE, text("删除用户表成功！"));
	}
	/**
	 * 导入用户数据
	 */
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport) {
		try {
			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			String message = zxUserService.importData(file, isUpdateSupport);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}
}